<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>字典管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
	
	//url
	var listUrl = '${ctx}/sys/dict/';
	var cardUrl = '${ctx}/sys/dict/form';
	
	var sortUrl = listUrl+'updateSort';
	var statusUrl = listUrl+"ajaxUpdateStatus";
	$(document).ready(function() {
	
	});
	
	function page(n,s){
		$("#pageNo").val(n);
		$("#pageSize").val(s);
		$("#searchForm").submit();
    	return false;
    }
		
	function add() { //新增记录
		var parentId = null;
		//debugger;
		var treeSelectNodes = parent.getSelectNode();
		if(treeSelectNodes!=null && treeSelectNodes.length>0){
			var treeNode = treeSelectNodes[0];
			parentId = treeNode.id;
			if(treeNode.hasChild){
				parentId = null;
			}
		}
		var url = cardUrl + '?opt=add';
		url = addUrlParam(url,'dictitem.id',parentId);
		parent.freshTabs(jQuery(window.parent.document).find('[link="iframeContent2"]'), url);
	}
	
	function edit(obj, isEdit) { //修改记录
		var curentTr = null;
		if (obj != null) {
			curentTr = jQuery(obj).parents('tr')[0];
		} else {
			var table = jQuery("#contentTable");
			var checkboxs = jQuery('input[attrname="sort"]:checked', table);
			if (checkboxs.length == 0) {
				alertx("请选择一行数据！");return;
			} else if (checkboxs.length > 1) {
				alertx("只能选择一行数据！");return;
			}
			curentTr = jQuery(checkboxs[0]).parents('tr')[0];
		}
		var id = jQuery('[attrname="id"]', curentTr).val();
		if (id != null || id != '') {
			var opt = (isEdit==true || isEdit == null) ? 'edit' : 'other';
			var url = cardUrl + '?opt=' + opt + '&id=' + id;
			parent.freshTabs(jQuery(window.parent.document).find('[link="iframeContent2"]'), url);
		}
	}
	
	function detail(obj) { //查看记录详情
		edit(obj, false);
	}
	
	function del(obj) { //删除记录
		if (obj != null) { //单个删除
			var curentTr = jQuery(obj).parents('tr')[0];
			var id = jQuery('[attrname="id"]', curentTr).val();
			var url = listUrl + 'delete?id=' + id;
			confirmx('确认要删除该赛事视频吗？', function() {
				ajaxDelete(id);
			});
		} else { //批量删除
			var table = jQuery("#contentTable");
			var checkboxs = jQuery('input[attrname="sort"]:checked', table);
			if (checkboxs.length == 0) {
				alertx("请选择至少一行数据！");
				return;
			}
			var ids = [];
			for (var i = 0; i < checkboxs.length; i++) {
				var curentTr = jQuery(checkboxs[i]).parents('tr')[0];
				var id = jQuery('[attrname="id"]', curentTr).val();
				ids.push(id);
			}
			var url = listUrl + 'batchDelete?ids=' + ids.join();
			confirmx('确认要删除所选赛事视频吗？', function() {
				ajaxDelete(ids.join());
			});
		}
	}
	
	function ajaxDelete(ids){
		var url = listUrl + 'ajaxDelete?rk='+Math.random();
		var data = {ids:ids};
	    jQuery.ajax({  
			url: url, async : false,type: "POST",
			data : data,
			success: function(response) {
				info = response;
				if(info.success == '1'){
					queryList();
				}
				alertx(info.message);
			},
			error: function(e){console.error(e);}
		});
	}
	
	function submitData(){ //提交表单
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if(formObj!=null){
			formObj.submit();
		}
	}
	
	function resetForm(obj) { //重置查询条件
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if (jQuery(formObj)[0] != null) {
			jQuery(formObj)[0].reset();
		}
	}
	
	function clearForm(obj){ //清空查询条件
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if (jQuery(formObj)[0] != null) {
			jQuery(formObj).find(':input').each(function(){ 
	            switch(this.type){  
	                case 'passsword':  
	                case 'select-multiple':  
	                case 'select-one':  
	                case 'text':  
	                case 'textarea':  
	                    jQuery(this).val('');  
	                    jQuery('.select2-chosen',formObj).html('');
	                    break;  
	                case 'checkbox':  
	                case 'radio':  
	                    this.checked = false;  
	            }  
	        });     
		}
	}
	
	function queryList(){
		var form = jQuery('#searchForm')[0];
		form.submit();
	}
	
	function updateSort() {
	  loading('正在提交，请稍等...');
	  var url = $("#listForm").attr("action");
	  $("#listForm").attr("action",sortUrl );
      $("#listForm").submit();
      $("#listForm").attr("action",url );
	}
	
	function ajaxUpateStatus(id,status){
		var url = statusUrl;
		var data = {id:id,status:status};
	    jQuery.ajax({  
			url: url, async : false,type: "POST",
			data : data,
			success: function(response) {
				info = response;
				if(info.success == '1'){
					queryList();
				}
				alertx(info.message);
			},
			error: function(e){console.error(e);}
		});
	}
	
</script>
<style type="text/css">
.tableHead th, td {
	text-align: center;
}
</style>
</head>
<body>
	<form:form id="searchForm" modelAttribute="dict" action="${ctx}/sys/dict/" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<label>类型：</label><form:select id="type" path="type" class="input-medium"><form:option value="" label=""/><form:options items="${typeList}" htmlEscape="false"/></form:select>
		&nbsp;&nbsp;<label>描述 ：</label><form:input path="description" htmlEscape="false" maxlength="50" class="input-medium"/>
		&nbsp;<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
	</form:form>
	<sys:message content="${message}"/>
	<div class="" style="margin-bottom: 3px;">
		<shiro:hasPermission name="sys:dictitem:edit">
			<input id="btnAdd" class="btn btn-primary" type="button" value="新 增" onclick="add()"/>&nbsp;
			<input id="btnEdit" class="btn btn-primary" type="button" value="修 改" onclick="edit()"/>&nbsp;
			<input id="btnDel" class="btn btn-primary" type="button" value="删 除" onclick="del()"/>&nbsp;
			<input id="btnSort" class="btn " type="button" value="保存排序" onclick="updateSort()"/>
		</shiro:hasPermission>
		<shiro:hasPermission name="sys:dictitem:view">
			<input id="btnDetail" class="btn " type="button" value="查 看" onclick="detail()"/>&nbsp;
		</shiro:hasPermission>	
	</div>
	<form id="listForm" method="post">
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr class="tableHead">
				<th style="text-align:center;" onclick="toggleCheckBox('checkboxName',this)">
		  			<input type="checkbox" checkboxName="sort" name="table_form_checkbox_all" value="">
		  		</th>
		  		<th style="text-align:center;">序号</th>
				<th>键值</th>
				<th>标签</th>
				<th>类型</th>
				<th>描述</th>
				<th>备注</th>
				<th>排序</th>
				<th>更新时间</th>
				<shiro:hasPermission name="sys:dict:edit">
				<th>操作</th>
				</shiro:hasPermission>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${page.list}" var="dict" varStatus="status">
			<tr>
				<td style="text-align:center;" >
					<input type="checkbox" name="sort" attrname="sort" value="">
					<input type="hidden" attrname="id" value="${dict.id}"/>
				</td>
				<td style="text-align:center;">
					<span attrname="number">${ status.index + 1}</span>
				</td>
				<td>${dict.value}</td>
				<td><a href="javascript:void(0);" onclick="detail(this)">${dict.label}</a></td>
				
				<td>${dict.type}</td>
				<td>${dict.description}</td>
				<td>${dict.remarks}</td>
				<td style="vertical-align: middle;text-align: center;">
					<input type="hidden" name="ids" value="${dict.id}"/>
					<input type="text" name="sorts" value="${dict.sort}" maxlength="50" class="required" style="width:50px;height: 14px;margin: 0px;"/>
				</td>
				<td>
					<fmt:formatDate value="${dictitem.updateDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
				</td>
				<shiro:hasPermission name="sys:dict:edit"><td>
    					<a href="javascript:void(0);" onclick="edit(this)">修改</a>
					<a href="javascript:void(0);" onclick="del(this)">删除</a>
				</td></shiro:hasPermission>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
	</form>
	<div style="height: 50px;width: 100%;"></div>
</body>
</html>